#!/usr/bin/perl -w
#   Written by Jimmy Saw - 10-11-2007
#   This program takes a Genbank file and extract protein sequence fastas
#   You have to redirect output to a file

use Bio::SeqIO;

my $seqio_obj = Bio::SeqIO->new (-file => $ARGV[0], -format => 'genbank');

my $seq_obj;
my @seq_array;

while ($seq_obj = $seqio_obj->next_seq()){
    push (@seq_array, $seq_obj);
}

my $total = 0;
my $countseq = 0;

#foreach my $seq (@seq_array){
#    $total += $seq->length;
#    $countseq++;
#}

foreach $seq_obj (@seq_array)
{
    my $count = 0;
    
#    print $seq_obj->display_name, "\n";

    for my $feat_obj ($seq_obj->get_SeqFeatures){

        $count++;

        if ($feat_obj->primary_tag eq "CDS"){

            if ($feat_obj->has_tag('locus_tag')){
                for my $value ($feat_obj->get_tag_values('locus_tag')){
                    print ">", $value, "\t";
                }
            }

            if ($feat_obj->has_tag('product')){
                for my $value ($feat_obj->get_tag_values('product')){
                    print ">", $seq_obj->display_name, "_", $count-1, "\t", $value, "\n";
                }
            }

            if ($feat_obj->has_tag('translation')){
                for my $value ($feat_obj->get_tag_values('translation')){
                    print $value, "\n";
                }
            }
        }
    }
}
